<?php 

namespace Zbsht\Controller;


class ArticleController extends CommentController{


	/**
	 * 文章列表
	 * @return [type] [description]
	 */
	public function articleList(){

		if (IS_AJAX && IS_GET) {
			$get = I('get.key');

			$where = getOrPostVal($get);
			$Article = D('ArticleRelation')->field(['id','title_zh','is_show','add_time','uid','article_type']);
			$pageCount = D('ArticleRelation');
			if (!empty($where)) {
				$Article->where($where);
			}


			$Article->relation(true)->order('id desc');
			$data = $Article->limit(I('get.page') == '1' ? '0' : I('get.page') * 10 - 10 ,10)->select();
			$page = $pageCount->where($where)->count();

			
			echo returnYlauiJsonData($data , $page);
		}else{

			$this->display('listform');
		}
	}




	/**
	 * 添加文章
	 */
	public function addArticle(){

		if (IS_POST && IS_AJAX) {
			$post = I('post.');
			$post['add_time'] = empty($post['add_time']) ? time() : strtotime($post['add_time'].' '.date('H:i:s'));

			$post['uid'] 	= session(C('USER_AUTH_KEY'));
			if (isset($post['is_show'])) {
				$post['is_show'] = 1;
			}


			// 如果有id 不为空就是 修改
			if (!empty($post['id'])) {
				if (!isset($post['is_show'])) {
					$post['is_show'] = null;
				}
				unset($post['add_time']);
				$post['edit_time'] = time();
				M('article')->save($post) ? $this->success('修改成功！') : $this->error('修改失败！');
			}else{
				if ($post['article_type'] == '3') {
					M('article')->where(['article_type'=>'3'])->save(['is_show'=>0]);
				}

				M('article')->add($post) ? $this->success('添加成功！') : $this->error('添加失败！');
			}
		}

		$this->display();
	}


	// 文章编辑
	public function edit(){

		$this->article = D('ArticleRelation')->where(['id'=>I('get.id')])->relation(true)->find();

		$this->display('addArticle');
	}


	// 文章删除
	public function del(){

		M('article')->delete(I('get.id')) ? $this->success('删除成功！') : $this->error('删除失败！');
	}












}



?>